package com.rc.saas.tenant.shiro;

import com.rc.saas.tenant.common.constant.ShiroConstants;
import com.rc.saas.tenant.model.platform.PlatformUser;
import com.rc.saas.tenant.model.tenant.TenantUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * Created by sven on 2017/2/27.
 */
public final class ShiroUtils {
    /**
     * 默认使用web容器(支持redis集中式session储存)
     * 将一些数据放到ShiroSession中,以便于其它地方使用
     * 比如Controller,使用时直接用HttpSession.getAttribute(key)就可以取到
     */
    public static void saveSession(Object key, Object value) {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Session session = subject.getSession();
            //System.out.println("Session默认超时时间为[" + session.getTimeout() + "]毫秒");

            if (session != null) {
                session.setAttribute(key, value);
            }
        }
    }

    public static TenantUser getSessionUser() {
        Session session = SecurityUtils.getSubject().getSession();
        TenantUser user = (TenantUser) session.getAttribute(ShiroConstants.SESSION_TENANT_USER_KEY);

        return user;
    }

}
